package libs

import (
	// "fmt"
	"log"
	"os"

	// "html/template"
	"text/template"
)

func NewPlist(host *HostInfo, app *AppInfo, destPath string) error {
	// 如果文件已存在，会将文件清空
	f, err := os.Create(destPath)
	if err != nil {
		log.Println("create file ios.plist error: ", err)
		return err
	}

	t, err := template.ParseFiles("./views/tpl.plist")
	if err != nil {
		log.Print("template pagorsing error: ", err)
		return err
	}

	data := map[string]interface{}{}
	data["host"] = host
	data["app"] = app
	data["xml_head"] = `<?xml version="1.0" encoding="UTF-8"?>`
	err = t.Execute(f, data)
	if err != nil {
		log.Print("execute: ", err)
		return err
	}

	return nil
}

func NewPlistPublish(host *HostInfo, app *AppInfo) error {
	path := "./static/publish.plist"
	f, err := os.Create(path)
	if err != nil {
		log.Println("create file ios.plist error: ", err)
		return err
	}

	t, err := template.ParseFiles("./views/tpl.plist")
	if err != nil {
		log.Print("template pagorsing error: ", err)
		return err
	}

	host.IpaPath = "https://scmapp.hspcn.com/app/publish/publish.ipa"
	host.IconPath = "https://scmapp.hspcn.com/app/publish/icon.png"

	data := map[string]interface{}{}
	data["host"] = host
	data["app"] = app
	data["xml_head"] = `<?xml version="1.0" encoding="UTF-8"?>`
	err = t.Execute(f, data)
	if err != nil {
		log.Print("execute: ", err)
		return err
	}

	return nil
}
